package com.hmyd.hmyd_android.ui;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.adapter.SecurityQuestionAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.utils.MyToast;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 重置安全问题
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_reset_security_question)
public class ResetSecurityIssuesActivity extends BasicActivity {
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.tv_securityquestion_question_one)
	private TextView mTvQuestionOne;
	@ViewInject(R.id.tv_securityquestion_question_two)
	private TextView mTvQuestionTwo;

	@ViewInject(R.id.ed_securityquestion_answer_one)
	private EditText mEdAnswerOne;
	@ViewInject(R.id.ed_securityquestion_answer_two)
	private EditText mEdAnswerTwo;

	@ViewInject(R.id.submit)
	private Button mButton;

	private Context mContext;
	private List<String> datas = new ArrayList<String>();
	private String questionOne;
	private String questionTwo;

	private final int RESETSECURITYQUESTION = 1;

	private String code;
	private String codeId;

	@OnClick({ R.id.tv_securityquestion_question_one, R.id.tv_securityquestion_question_two,
			R.id.submit })
	public void onClick(View view) {
		switch (view.getId()) {
		// 选中问题一
		case R.id.tv_securityquestion_question_one:
			showQuestionOne();
			break;
		// 选中问题二
		case R.id.tv_securityquestion_question_two:
			showQuestionTwo();
			break;
		// 提交
		case R.id.submit:
			submit();
			break;
		default:
			break;
		}
	}

	public static Intent getIntent(Context context, String code, String codeId) {
		Intent intent = new Intent(context, SecurityIssuesActivity.class);
		intent.putExtra("code", code);
		intent.putExtra("codeId", codeId);

		return intent;
	}

	private void submit() {
		String answer1 = mEdAnswerOne.getText().toString().trim();
		String answer2 = mEdAnswerTwo.getText().toString().trim();

		if (TextUtils.isEmpty(questionOne) || TextUtils.isEmpty(answer1)) {
			MyToast.showHMToast(this, false, "请选择问题一并回答");
			return;
		}

		if (TextUtils.isEmpty(questionTwo) || TextUtils.isEmpty(answer2)) {
			MyToast.showHMToast(this, false, "请选择问题二并回答");
			return;
		}

		if (questionOne.equals(questionTwo)) {
			MyToast.showHMToast(this, false, "问题一和问题二不能相同");
			return;
		}

		String[] questions_en = getResources().getStringArray(R.array.question_en);
		String[] question = getResources().getStringArray(R.array.question);

		for (int i = 0; i < questions_en.length; i++) {
			if (questionOne.equals(question[i])) {
				questionOne = questions_en[i];
			}

			if (questionTwo.equals(question[i])) {
				questionTwo = questions_en[i];
			}
		}

		RequestParams params = new RequestParams();
		params.addBodyParameter("code1", questionOne);
		params.addBodyParameter("code2", questionTwo);
		params.addBodyParameter("answer1", answer1);
		params.addBodyParameter("answer2", answer2);
		params.addBodyParameter("mobileSessionId", codeId);
		params.addBodyParameter("mobileCode", code);

		onRequest(RESETSECURITYQUESTION, UrlConstant.RESETSECURITYQUESTION, HttpMethod.POST, params);
	}

	@Override
	public void onPostSuccess(int postId, String result) {
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.setClass(mContext, SecurityCenterActivity.class);
		startActivity(intent);
		finish();
	}

	private void showQuestionTwo() {

		View contentView = LayoutInflater.from(mContext).inflate(R.layout.view_security_question,
				null);
		ListView listView = (ListView) contentView.findViewById(R.id.lv);
		listView.setSelector(R.color.home_tab_text_pressed);
		SecurityQuestionAdapter adapter = new SecurityQuestionAdapter(mContext, datas);
		listView.setAdapter(adapter);
		final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);
		popupWindow.setTouchable(true);

		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return false;
			}
		});
		popupWindow.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.security_question_pop_bg));
		popupWindow.showAsDropDown(mTvQuestionTwo);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@SuppressLint("ShowToast")
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				questionTwo = (String) parent.getItemAtPosition(position);
				mTvQuestionTwo.setText(questionTwo);
				popupWindow.dismiss();
			}
		});
	}

	private void showQuestionOne() {

		View contentView = LayoutInflater.from(mContext).inflate(R.layout.view_security_question,
				null);
		ListView listView = (ListView) contentView.findViewById(R.id.lv);
		listView.setSelector(R.color.home_tab_text_pressed);
		SecurityQuestionAdapter adapter = new SecurityQuestionAdapter(mContext, datas);
		listView.setAdapter(adapter);

		final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, true);
		popupWindow.setTouchable(true);

		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return false;
			}
		});
		popupWindow.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.security_question_pop_bg));
		popupWindow.showAsDropDown(mTvQuestionOne);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@SuppressLint("ShowToast")
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				questionOne = (String) parent.getItemAtPosition(position);
				mTvQuestionOne.setText(questionOne);
				popupWindow.dismiss();
			}
		});
	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			if (!TextUtils.isEmpty(s.toString())) {
				mButton.setEnabled(true);
			}
		}
	};

	@Override
	protected void setParams() {
		home_title_txt.setText("重置安全问题");
		home_title.setOnTouchListener(touch);

		codeId = getIntent().getStringExtra("codeId");
		code = getIntent().getStringExtra("code");
		mContext = this;

		String[] questions = getResources().getStringArray(R.array.question);
		datas = Arrays.asList(questions);

		mEdAnswerOne.addTextChangedListener(watcher);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

}
